package net.src.socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端tcp通信
 */
public class ServerDemo {
    public static void main(String[] args) throws Exception {
        //注册端口
        ServerSocket serverSocket = new ServerSocket(7777);
        //必须调用accept方法,等待接收客户端Socket链接请求,建立Socket通信管道
        Socket socket = serverSocket.accept();
        //获取字节输入流
        InputStream is = socket.getInputStream();

        //包装成缓冲字节输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String msg;
        if((msg = br.readLine())!=null){
            System.out.println(socket.getRemoteSocketAddress() + "发送了:" + msg);
        }
    }
}
